package ylg.com.cn.beuty.video;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.widget.VideoView;

/**
 * 自动全屏的VideoView
 * https://github.com/Dawish/MDPlayer/tree/master/app
 */
public class FullScreenVideoView extends VideoView {

	private int videoWidth;
	private int videoHeight;

	public FullScreenVideoView(Context context) {
		super(context);
	}

	public FullScreenVideoView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	/**
	 * 重要的方法 自己计算合适的宽高
	 * @param newWidth
	 * @param newHeight
	 * @param maxWidth
	 * @param maxHeight
	 * @return
	 */
	public float[] countHeight(int newWidth ,int newHeight,int maxWidth,int maxHeight) {

		float rightWidth;
		float rightHight;
		
		if (maxHeight*newWidth<newHeight*maxWidth) {
			// 判断 有没有超过最大的高度
			rightHight= maxHeight;
			// 长视频  高度做为优先级
			float rate = (float)newHeight/rightHight;
			rightWidth= newWidth/rate;
		}else{
			rightWidth = maxWidth;
			float rate = (float)newWidth/rightWidth;
			rightHight= newHeight/rate;
		}
		float widthHeight[] = {rightWidth,rightHight};
		LayoutParams vlp = getLayoutParams();

		vlp.width = (int)  rightWidth;
		vlp.height = (int) rightHight;
		return widthHeight;
	}
/*	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int width = getDefaultSize(videoWidth, widthMeasureSpec);
		int height = getDefaultSize(videoHeight, heightMeasureSpec);
		if (videoWidth > 0 && videoHeight > 0) {
			if (videoWidth * height > width * videoHeight) {
				height = width * videoHeight / videoWidth;
			} else if (videoWidth * height < width * videoHeight) {
				width = height * videoWidth / videoHeight;
			}
		}
		setMeasuredDimension(width, height);
		AppLog.e("height"+ videoWidth);
		AppLog.e("width"+videoHeight);
		setMeasuredDimension(videoWidth, videoHeight);
	}*/
	 @Override
	    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	            // TODO Auto-generated method stub
	            // Log.i("@@@@", "onMeasure");
	           //下面的代码是让视频的播放的长宽是根据你设置的参数来决定
	            int width = getDefaultSize(videoWidth, widthMeasureSpec);
	            int height = getDefaultSize(videoHeight, heightMeasureSpec);
	            setMeasuredDimension(width, height);
	    }
	public int getVideoWidth() {
		return videoWidth;
	}

	public void setVideoWidth(int videoWidth) {
		this.videoWidth = videoWidth;
	}

	public int getVideoHeight() {
		return videoHeight;
	}

	public void setVideoHeight(int videoHeight) {
		this.videoHeight = videoHeight;
	}

}
